<?
$sub_menu = "230460";
include_once("./_common.php");

auth_check($auth[$sub_menu], "w");

check_token();

if ($w == "")
{
    $membership = sql_fetch("select * from $mw_cash[membership_table] where ms_id = '$ms_id'");
    if (!$membership)
        alert("멤버쉽이 존재하지 않습니다.");

    $tmp = explode(',', $mb_id);
    $tm2 = array();
    for ($i=0, $m=count($tmp); $i<$m; $i++) {
        $mb_id = trim($tmp[$i]);
        if (!$mb_id) continue;

        $row = sql_fetch("select * from $g4[member_table] where mb_id = '$mb_id'");
        if (!$row)
            alert("$mb_id 회원ID 가 존재하지 않습니다.");

        $tm2[] = $mb_id;
    }

    for ($i=0, $m=count($tm2); $i<$m; $i++) {
        $mb_id = $tm2[$i];

        if (!$_POST[ml_sdate]) {
            $sql = " select max(ml_edate) as edate from $mw_cash[membership_log_table] ";
            $sql.= " where mb_id = '$mb_id' and ms_id = '$ms_id' and ml_edate >= '$g4[time_ymdhis]' ";
            $row = sql_fetch($sql);
            if ($row[edate])
                $ml_sdate = date("Y-m-d H:i:s", strtotime($row[edate]) + 1);
            else
                $ml_sdate = $g4[time_ymdhis];
        }
        else
            $ml_sdate = date("Y-m-d H:i:s", strtotime($ml_sdate));

        if (!$_POST[ml_edate])
            $ml_edate = date("Y-m-d H:i:s", strtotime($ml_sdate) + (60*60*24*$ml_day));
        else
            $ml_edate = date("Y-m-d H:i:s", strtotime($ml_edate));

        $cl_id = mw_buy_contents2($mb_id, $ml_cash, "$membership[ms_name] 멤버쉽 ($ml_edate 종료)"); 

        $sql = "insert into $mw_cash[membership_log_table] set 
                    ms_id = '$ms_id'
                ,   cl_id = '$cl_id'
                ,   mb_id = '$mb_id'
                ,   ml_cash = '$ml_cash'
                ,   ml_day = '$ml_day'
                ,   ml_sdate = '$ml_sdate'
                ,   ml_edate = '$ml_edate'
                ,   ml_datetime = '$g4[time_ymdhis]'";
        $qry = sql_query($sql);
        $ml_id = mysql_insert_id();
    }

    if ($m == 1)
        $url = "mw.membership.log.form.php?w=u&ml_id=$ml_id$qstr";
    else
        $url = "mw.membership.log.php?$qstr";

    $msg = "등록";
}
else if ($w == "u" && $ml_id)
{
    $row = sql_fetch("select * from $mw_cash[membership_log_table] where ml_id = '$ml_id'");
    if (!$row)
	alert("자료가 없습니다.");

    if (!trim($ml_edate))
        $ml_edate = date("Y-m-d H:i:s", strtotime($ml_sdate) + (60*60*24*$ml_day));

    $sql = "update $mw_cash[membership_log_table] set 
                ms_id = '$ms_id'
            ,   ml_day = '$ml_day'
            ,   ml_sdate = '$ml_sdate'
            ,   ml_edate = '$ml_edate'
            where ml_id = '$ml_id'";
    $qry = sql_query($sql);

    $url = "mw.membership.log.form.php?w=u&ml_id=$ml_id$qstr";
    $msg = "수정";
}
else if ($w == "d" && $ml_id)
{
    $row = sql_fetch("select * from $mw_cash[membership_log_table] where ml_id = '$ml_id'");
    if (!$row)
        alert("자료가 없습니다.");

    mw_cash_delete($row[cl_id]);
    
    sql_query("delete from $mw_cash[membership_log_table] where ml_id = '$ml_id'");

    $url = "mw.membership.log.php?$qstr";
    $msg = "삭제";
}

alert("$msg 되었습니다.", $url);
?>
